//
//  HTDeliverTruckTypeView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/2.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDeliverTruckTypeView.h"
#import "HTDeliverTruckBoardView.h"
#define boardHeight kWidth(500)
@interface HTDeliverTruckTypeView()

@property (nonatomic,strong) HTDeliverTruckBoardView *deliverTruckBoardView;
@property (nonatomic,strong) UIView *backView;

@end

@implementation HTDeliverTruckTypeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self setupView];
    }
    return self;
}
- (void)setupView{
    
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, (kHTScreenHeight - boardHeight))];
    self.backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [self addSubview:self.backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [self.backView addGestureRecognizer:tap];
    [self addSubview:self.deliverTruckBoardView];
    [self.deliverTruckBoardView.closeButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    [self.deliverTruckBoardView.conformButton addTarget:self action:@selector(didConform) forControlEvents:UIControlEventTouchUpInside];
}
- (void)didConform{
    NSString *length = @"";
    NSInteger index = 0;
    for (int i= 0; i < self.deliverTruckBoardView.selectedLengths.count; i++) {
        HTTruckTypeModel *model = self.deliverTruckBoardView.selectedLengths[i];
        if (!(model.truckTypeModelType == TruckTypeModelTypeUnlimited)) {
            if (index < 2) {
                length = [NSString stringWithFormat:@"%@,%@",length,model.title];
                index ++;
            }
        }
    }
    if (length.length <= 0) {
        length = @"不限车长";
    }else{
         length = [length substringFromIndex:1];
    }
    NSString *truckString = @"";
    if (!( self.deliverTruckBoardView.selectedTruckType.truckTypeModelType == TruckTypeModelTypeUnlimited)) {
            truckString = [NSString stringWithFormat:@"%@",self.deliverTruckBoardView.selectedTruckType.title];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoiceLengths:truckType:isLessThan:)]) {
        [self.delegate didChoiceLengths:self.deliverTruckBoardView.selectedLengths truckType:truckString isLessThan:self.deliverTruckBoardView.isLessThanTruck];
    }
    [self dismissView];
}

- (HTDeliverTruckBoardView *)deliverTruckBoardView{
    if (!_deliverTruckBoardView) {
        _deliverTruckBoardView = [[NSBundle mainBundle]loadNibNamed:@"HTDeliverTruckBoardView" owner:self options:nil].firstObject;
        _deliverTruckBoardView.frame = CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
    }
    return _deliverTruckBoardView;
}

-(void)showView{
    self.deliverTruckBoardView.frame =  CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.deliverTruckBoardView.frame =  CGRectMake(0, kHTScreenHeight - boardHeight, kHTScreenWidth, boardHeight);
        [self layoutIfNeeded];
    }];
}
-(void)dismissView {
    
    [UIView animateWithDuration:.3 animations:^{
        self.deliverTruckBoardView.frame =  CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
        self.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}


@end
